﻿<%@ WebHandler Language="C#" Class="FoodHandler" %>

using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using Dln.Common;
using Dln.Common.Helper;

using System.Linq;
using System.Web.UI.WebControls;

public class FoodHandler : HandlerBase
{
    /// <summary>
    /// 获取当日菜普分类
    /// </summary>
    public void GetFoodList()
    {
        string FoodPart = _httpRequestForm["FoodPart"];
        string FoodDate = _httpRequestForm["FoodDate"];
        if (!string.IsNullOrEmpty(FoodPart) && !string.IsNullOrEmpty(FoodDate))
        {
            FoodDate = Week(Convert.ToDateTime(FoodDate).DayOfWeek + "");
            //var url = CommonHelper.GetAppSettings("DlnService1");
            //object[] args = new object[2];
            //args[0] = FoodDate;
            //args[1] = Convert.ToInt32(FoodPart);
            //object result = WebServiceHelper.InvokeWebService(url, "GetFoods", args);
            //if (result != null)
            //{
            //    DataSet ds = (DataSet)result;

            //    var dc = ds.Tables[0];
            //    if (!dc.Columns.Contains("ResultDataSet"))
            //    {
            //        var results = dc.Rows.Cast<DataRow>().Select(dr => dr["FOODTYPE"].ToString()).Distinct().OrderBy(name => name).ToList();
            //        HttpContext.Current.Response.Write(JSONHelper.ObjectToJson(results));
            //        HttpContext.Current.Response.End();
            //    }
            //}
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            object result = client2.GetFoods(FoodDate, Convert.ToInt32(FoodPart));
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (!dc.Columns.Contains("ResultDataSet"))
                {
                    var results = dc.Rows.Cast<DataRow>().Select(dr => dr["FOODTYPE"].ToString()).Distinct().OrderBy(name => name).ToList();
                    HttpContext.Current.Response.Write(JSONHelper.ObjectToJson(results));
                    HttpContext.Current.Response.End();
                }
            }
        }
    }

    /// <summary>
    /// 根据分类获取菜品
    /// </summary>
    public void GetFoodType()
    {
        string FoodPart = _httpRequestForm["FoodPart"];
        string FoodDate = _httpRequestForm["FoodDate"];
        string foodType = _httpRequestForm["FoodType"];
        if (!string.IsNullOrEmpty(FoodPart) && !string.IsNullOrEmpty(FoodDate))
        {
            FoodDate = Week(Convert.ToDateTime(FoodDate).DayOfWeek + "");
            //var url = CommonHelper.GetAppSettings("DlnService1");
            //object[] args = new object[2];
            //args[0] = FoodDate;
            //args[1] = Convert.ToInt32(FoodPart); ;
            //object result = WebServiceHelper.InvokeWebService(url, "GetFoods", args);
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            object result = client2.GetFoods(FoodDate, Convert.ToInt32(FoodPart));
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (dc != null && dc.Rows.Count > 0)
                {
                    var a = dc.Rows.Cast<DataRow>().Where(dr => dr["FOODTYPE"].ToString() == foodType).CopyToDataTable();
                    HttpContext.Current.Response.Write(SqlHelper.GetTablejson(a));
                    HttpContext.Current.Response.End();
                }


            }
        }
    }

    /// <summary>
    /// 获取当天必吃的菜
    /// </summary>
    public void GetFoodsBiChi()
    {
        string FoodPart = _httpRequestForm["FoodPart"];
        string FoodDate = _httpRequestForm["FoodDate"];
        if (!string.IsNullOrEmpty(FoodPart) && !string.IsNullOrEmpty(FoodDate))
        {
            FoodDate = Week(Convert.ToDateTime(FoodDate).DayOfWeek + "");
            //var url = CommonHelper.GetAppSettings("DlnService1");
            //object[] args = new object[2];
            //args[0] = FoodDate;
            //args[1] = Convert.ToInt32(FoodPart); ;
            //object result = WebServiceHelper.InvokeWebService(url, "GetFoods", args);
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            object result = client2.GetFoods(FoodDate, Convert.ToInt32(FoodPart));
            if (result != null)
            {
                DataSet ds = (DataSet)result;

                var dc = ds.Tables[0];
                if (dc != null && dc.Rows.Count > 0)
                {
                    var MEANID = dc.Rows.Cast<DataRow>().Where(dr => dr["FOODSTATE"].ToString() == "1").Select(dr => dr["MEANID"].ToString()).ToList();

                    HttpContext.Current.Response.Write(JSONHelper.ObjectToJson(MEANID));
                    HttpContext.Current.Response.End();
                }


            }
        }
    }

    public void OrderDinner()
    {
        string MeanIDList = _httpRequestForm["MeanIDList"];
        string FoodDate = _httpRequestForm["FoodDate"];
        string FoodPart = _httpRequestForm["FoodPart"];
        string CardID = _httpRequestForm["CardID"];
        if (!string.IsNullOrEmpty(MeanIDList) && !string.IsNullOrEmpty(FoodDate) && !string.IsNullOrEmpty(FoodPart) && !string.IsNullOrEmpty(CardID))
        {
            //var url = CommonHelper.GetAppSettings("DlnService1");
            //object[] args = new object[4];
            //args[0] = CardID;
            //args[1] = MeanIDList;
            //args[2] = Convert.ToDateTime(FoodDate).ToShortDateString();
            //args[3] = FoodPart;
            //object result = WebServiceHelper.InvokeWebService(url, "OrderDinner", args);
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            object result = client2.OrderDinner(CardID, MeanIDList,Convert.ToDateTime(FoodDate).ToShortDateString(),FoodPart);
            if (result != null)
            {
                if ((result + "") == "0")
                {
                    PrintErrorJson("患者没有住院或未开立饮食医嘱");
                }
                else if ((result + "") == "1" || (result + "") == "3")
                {
                    PrintErrorJson("订餐失败，请联系床位护士");
                }
                else if ((result + "") == "4")
                {
                    PrintErrorJson("订餐失败，已存在相同餐次！");
                }
                else
                {
                    PrintSuccessJson("订餐成功");
                }
            }
        }
        else
        {
            PrintErrorJson("订餐失败，未查到该餐次信息，请联系床位护士");
        }

    }


    public void DelOrderDinner()
    {
        string FoodDate = _httpRequestForm["FoodDate"];
        string FoodPart = _httpRequestForm["FoodPart"];
        string CardID = _httpRequestForm["CardID"];
        if (!string.IsNullOrEmpty(CardID) && !string.IsNullOrEmpty(FoodDate) && !string.IsNullOrEmpty(FoodPart))
        {
            //var url = CommonHelper.GetAppSettings("DlnService1");
            //object[] args = new object[3];
            //args[0] = CardID;
            //args[1] = Convert.ToDateTime(FoodDate).ToShortDateString();
            //args[2] = FoodPart;
            //object result = WebServiceHelper.InvokeWebService(url, "DelOrderDinner", args);
            //if (result != null)
            //{
            //    if ((result + "") == "0")
            //    {
            //        PrintErrorJson("未查到该患者在院信息");
            //    }
            //    else if ((result + "") == "1" || (result + "") == "3")
            //    {
            //        PrintErrorJson("取消失败，请联系床位护士");
            //    }
            //    else
            //    {
            //        PrintSuccessJson("取消成功");
            //    }
            //}
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            object result = client2.DelOrderDinner(CardID, Convert.ToDateTime(FoodDate).ToShortDateString(), FoodPart);
            if (result != null)
            {
                if ((result + "") == "0")
                {
                    PrintErrorJson("未查到该患者在院信息");
                }
                else if ((result + "") == "1" || (result + "") == "3")
                {
                    PrintErrorJson("取消失败，请联系床位护士");
                }
                else
                {
                    PrintSuccessJson("取消成功");
                }
            }
        }
        else
        {
            PrintErrorJson("取消失败，未查到该餐次信息，请联系床位护士");
        }

    }

    public string Week(string weekName)
    {
        string week = "";
        switch (weekName)
        {
            case "Sunday":
                week = "06";
                break;
            case "Monday":
                week = "00";
                break;
            case "Tuesday":
                week = "01";
                break;
            case "Wednesday":
                week = "02";
                break;
            case "Thursday":
                week = "03";
                break;
            case "Friday":
                week = "04";
                break;
            case "Saturday":
                week = "05";
                break;
        }
        return week;
    }
}